package zust.javaee.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import zust.javaee.dto.AjaxReturn;
import zust.javaee.dto.SessionInfo;
import zust.javaee.dto.admin.*;
import zust.javaee.dto.adminadd.CorporationAddDto;
import zust.javaee.dto.adminadd.PlayerAddDto;
import zust.javaee.enums.player.PlayerStatusEnum;
import zust.javaee.service.AdminServive;
import zust.javaee.service.CorporationService;
import zust.javaee.service.PlayerService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * Created by liy on 2016/12/18.
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private PlayerService playerService;

    @Autowired
    private AdminServive adminServive;

    @Autowired
    private CorporationService corporationService;

    @RequestMapping(value = "/login")
    public String login(){
        return "admin/login";
    }


    @RequestMapping(value = "/dologin")
    @ResponseBody
    public AjaxReturn<SessionInfo> dologin(
            HttpServletRequest request,
            HttpSession session,
            @RequestParam(value = "studentId", required = false) String studentId,
            @RequestParam(value = "password", required = false) String password) {
            return adminServive.doLogin(studentId, password, session);
    }

    @RequestMapping(value = "/index")
    public String indexs(HttpServletRequest request){
        request.setAttribute("playerNum", adminServive.getPlayerNum());
        request.setAttribute("corporNum", adminServive.getCorporationNum());
        Long ave = adminServive.getPlayerNum()/adminServive.getCorporationNum();
        request.setAttribute("ave", ave);
        return "admin/index";
    }

    /**
     * addsn们 页面
     * @return
     */
    @RequestMapping(value = "/player/add")
    public String addPlayer() {
        return "admin/player/add";
    }
    @RequestMapping(value = "/activity/add", method = RequestMethod.GET)
    public String addActivity() {
        return "admin/activity/add";
    }
    @RequestMapping(value = "/announcement/add", method = RequestMethod.GET)
    public String addAnnouncement() {
        return "admin/announcement/add";
    }
    @RequestMapping(value = "/corporation/add", method = RequestMethod.GET)
    public String addCorporation() {
        return "admin/corporation/add";
    }
    /**
     * addsn们 do
     * @return
     */
    @RequestMapping(value = "/player/add", method = RequestMethod.POST)
    public String doAddPlayer(
            PlayerAddDto playerAddDto,
            @RequestParam(value = "number", required = false) String number,
            @RequestParam(value = "datepicker", required = false) String datepicker) {
        playerAddDto.setBirthStr(datepicker);
        playerAddDto.setStudentId(number);
        System.out.println(playerAddDto.toString());
        adminServive.addPlayer(playerAddDto);
        return "redirect:list";
    }
    @RequestMapping(value = "/activity/add", method = RequestMethod.POST)
    @ResponseBody
    public String doAddactivity() {
        return "admin/activity/add";
    }
    @RequestMapping(value = "/announcement/add", method = RequestMethod.POST)
    @ResponseBody
    public String doAddannouncement() {
        return "admin/announcement/add";
    }
    @RequestMapping(value = "/corporation/add", method = RequestMethod.POST)
    @ResponseBody
    public String doAddcorporation() {
        return "admin/corporation/add";
    }


    /**
     * update 们
     */

    @RequestMapping(value = "/player/{id}/update")
    public ModelAndView updatePlayer(
            @PathVariable Integer id) {
        ModelAndView mv = new ModelAndView("admin/player/update");
        mv.addObject("player", adminServive.getById(id));
        return mv;
    }

    @RequestMapping(value = "/corporation/{id}/update")
    public ModelAndView updateCorpor(
            @PathVariable Integer id) {
        ModelAndView mv = new ModelAndView("admin/corporation/update");
        mv.addObject("corporation", corporationService.getById(id));
        return mv;
    }

    /**
     * update do们
     */
    @RequestMapping(value = "/player/{id}/update", method = RequestMethod.POST)
    public String doAddPlayer(
            @PathVariable Integer id,
            PlayerAddDto playerAddDto,
            @RequestParam(value = "number", required = false) String number,
            @RequestParam(value = "datepicker", required = false) String datepicker) {
        playerAddDto.setBirthStr(datepicker);
        playerAddDto.setStudentId(number);

        adminServive.updatePlayer(id, playerAddDto);
        return "redirect:/admin/player/list";
    }

    @RequestMapping(value = "/corporation/{id}/update", method = RequestMethod.POST)
    public String doUpdateCorpor(
            @PathVariable Integer id,
            CorporationAddDto corporationAddDto) {
        adminServive.updateCorporation(id, corporationAddDto);
        return "redirect:/admin/corporation/list";
    }

    /**
     * lists 们
     */

    @RequestMapping(value = "/player/list", method = RequestMethod.GET)
    public ModelAndView playerList(
            HttpServletRequest request,
            SessionInfo sessionInfo){
        ModelAndView mv = new ModelAndView("admin/player/list");
        List<PlayerListDto> players = adminServive.getPlayerIndexList(PlayerStatusEnum.AVAILABLE.getStatus());
        mv.addObject("players", players);
        return mv;
    }
    @RequestMapping(value = "/player/listQ", method = RequestMethod.GET)
    public ModelAndView playerListQ(
            HttpServletRequest request,
            SessionInfo sessionInfo){
        ModelAndView mv = new ModelAndView("admin/player/list_question");
        List<PlayerListDto> players = adminServive.getPlayerIndexListExcept((short) 1);
        mv.addObject("players", players);
        return mv;
    }

    @RequestMapping(value = "/corporation/list", method = RequestMethod.GET)
    public ModelAndView corporList(
            HttpServletRequest request,
            SessionInfo sessionInfo){
        ModelAndView mv = new ModelAndView("admin/corporation/list");
        List<CorporListDto> corporListDtos = adminServive.getCorporIndexList((short) 1);
        mv.addObject("corporations", corporListDtos);
        return mv;
    }

    @RequestMapping(value = "/corporation/listQ", method = RequestMethod.GET)
    public ModelAndView corporListQ(
            HttpServletRequest request,
            SessionInfo sessionInfo){
        ModelAndView mv = new ModelAndView("admin/corporation/list_question");
        List<CorporListDto> corporListDtos = adminServive.getCorporIndexListExcept((short) 1);
        mv.addObject("corporations", corporListDtos);
        return mv;
    }

    @RequestMapping(value = "/activity/list", method = RequestMethod.GET)
    public ModelAndView activityList(
            HttpServletRequest request,
            SessionInfo sessionInfo){
        ModelAndView mv = new ModelAndView("admin/activity/list");
        List<ActivityListDto> activityListDtos = adminServive.getActivityIndexList((short) 1);
        mv.addObject("activities", activityListDtos);
        return mv;
    }
    @RequestMapping(value = "/activity/listC", method = RequestMethod.GET)
    public ModelAndView activityListC(
            HttpServletRequest request,
            SessionInfo sessionInfo){
        ModelAndView mv = new ModelAndView("admin/activity/listC");
        List<ActivityListDto> activityListDtos = adminServive.getActivityIndexList((short) 0);
        mv.addObject("activities", activityListDtos);
        return mv;
    }
    @RequestMapping(value = "/activity/listP", method = RequestMethod.GET)
    public ModelAndView activityListP(
            HttpServletRequest request,
            SessionInfo sessionInfo){
        ModelAndView mv = new ModelAndView("admin/activity/listP");
        List<ActivityListDto> activityListDtos = adminServive.getActivityIndexList((short) 2);
        mv.addObject("activities", activityListDtos);
        return mv;
    }
    @RequestMapping(value = "/activity/listQ", method = RequestMethod.GET)
    public ModelAndView activityListQ(
            HttpServletRequest request,
            SessionInfo sessionInfo){
        ModelAndView mv = new ModelAndView("admin/activity/listQ");
        List<ActivityListDto> activityListDtos = adminServive.getActivityIndexListLess((short) 0);
        mv.addObject("activities", activityListDtos);
        return mv;
    }

    @RequestMapping(value = "/signcorpor/list", method = RequestMethod.GET)
    public ModelAndView signcorporList(
            HttpServletRequest request,
            SessionInfo sessionInfo){
        ModelAndView mv = new ModelAndView("admin/signcorpor/list");
        List<SignCorporListDto> signCorporListDtos = adminServive.getSignCorporIndexList((short) 0);
        mv.addObject("signCorpors", signCorporListDtos);
        return mv;
    }

    @RequestMapping(value = "/announcement/list", method = RequestMethod.GET)
    public ModelAndView announcmentList(
            HttpServletRequest request,
            SessionInfo sessionInfo){
        ModelAndView mv = new ModelAndView("admin/announcement/list");
        List<AnnouncementListDto> announcementListDtos = adminServive.getAnnouncementIndexList((short) -1);
        mv.addObject("announcements", announcementListDtos);
        return mv;
    }


    /**
     * check
     */
    @RequestMapping(value = "/checkStuId/{studentId}", method = RequestMethod.POST)
    @ResponseBody
    public boolean checkStudentId(
            @PathVariable Integer studentId) {
        if (playerService.checkExitPlayer(studentId))
            return false;
        else
            return true;
    }

    /**
     * change status 们
     */
    @RequestMapping(value = "/player/{playerId}/statu/{statu}", method = RequestMethod.POST)
    @ResponseBody
    public boolean changePlayerStatu(
            @PathVariable Integer playerId,
            @PathVariable Short statu) {
        return adminServive.changePlayerStatu(playerId, statu);
    }

    @RequestMapping(value = "/signcorpor/{id}/statu/{statu}", method = RequestMethod.POST)
    @ResponseBody
    public boolean changesignCorporStatu(
            @PathVariable Integer id,
            @PathVariable Short statu) {
        if (statu == 1)
            adminServive.passSignCorpor(id);
        return adminServive.changeSigncorporStatu(id, statu);
    }



    @RequestMapping(value = "/corporation/{id}/statu/{statu}", method = RequestMethod.POST)
    @ResponseBody
    public boolean changeCorporationStatu(
            @PathVariable Integer id,
            @PathVariable Short statu) {
        return adminServive.changeCorporationStatu(id, statu);
    }

    @RequestMapping(value = "/activity/{playerId}/statu/{statu}", method = RequestMethod.POST)
    @ResponseBody
    public boolean changeActivityStatu(
            @PathVariable Integer playerId,
            @PathVariable Short statu) {
        return adminServive.changeActivityStatu(playerId, statu);
    }

    @RequestMapping(value = "/announcement/{playerId}/statu/{statu}", method = RequestMethod.POST)
    @ResponseBody
    public boolean changeAnnouncementStatu(
            @PathVariable Integer playerId,
            @PathVariable Short statu) {
        return adminServive.changeAnnouncementStatu(playerId, statu);
    }

}
